<template>
    <div class="withdrawal">
        <Layout>
            <Header class="header">
                <Row>
                    <Col :span="24">
                        <div>
                            <span class="border-left"></span>
                            提现记录
                        </div>
                    </Col>
                </Row>
            </Header>
            <Content class="content">
                <Row>
                    <Col class="selectWay">
                        <Button :class="way == 1 ? 'current' : ''" @click="current(1)">支付宝</Button>
                        <Button @click="current(2)" :class="way == 2  ? 'current' : ''">微&nbsp;&nbsp;信</Button>
                    </Col>
                </Row>

                <div class="search-box">
                    <Row :gutter="16" style="min-width: 1024px">
                        <Col :span="6">
                            用户名称：
                            <Input v-model="userName" placeholder="用户名称" style="width:auto"/>
                        </Col>
                        <Col :span="6">
                            <div class="search-btn" @click="getList()">查询</div>
                        </Col>
                    </Row>
                </div>

                <!-- <div class="btn-box">
                    <Button type="primary" class="btn" @click="config">审核设置</Button>
                    <div>
                        审核开关：
                        <el-switch
                            v-model="isOpen"
                            active-color="#13ce66"
                            inactive-color="#ddd"
                            :disabled="openChangeLoading"
                            @change="openChange()">
                        </el-switch>
                        <i class="el-icon-loading" v-show="openChangeLoading" style="color:#13ce66;font-size:14px"></i>
                        <span v-show="!openChangeLoading">{{isOpen ? '（已开启）' : '（已关闭）'}}</span>
                    </div>
                    
                    <Button type="primary" style="margin-left:20px" class="btn" :disabled="tableData.length === 0" @click="exportData()">导出数据</Button>
                </div> -->

                <div class="table-box">
                    <el-table
                        ref="table"
                        :data="tableData"
                        size="mini"
                        style="width: 100%;min-width: 800px">
                        <!-- <el-table-column
                            type="selection"
                            width="55">
                        </el-table-column> -->
                        <el-table-column
                            align="center"
                            prop="uid"
                            label="用户ID">
                        </el-table-column>
                        <el-table-column
                            align="center"
                            label="用户昵称">
                            <template slot-scope="scope">
                                <p>{{scope.row.user_info && scope.row.user_info.name ? scope.row.user_info.name: '- -'}}</p>
                            </template>
                        </el-table-column>
                        <el-table-column
                            align="center"
                            label="提现金额">
                            <template slot-scope="scope">
                                <p>￥{{scope.row.amount}}</p>
                            </template>
                        </el-table-column>
                        <!-- <el-table-column
                            align="center"
                            label="提现渠道">
                            <template>
                                <p>{{111111}}</p>
                            </template>
                        </el-table-column> -->
                        <el-table-column
                            align="center"
                            label="提现账户">
                            <template slot-scope="scope">
                                <p>{{scope.row.user_info ? scope.row.user_info.phone : '- -'}}</p>
                            </template>
                        </el-table-column>
                        <el-table-column
                            align="center"
                            label="提现时间"
                            min-width="120">
                            <template slot-scope="scope">
                                <p>{{scope.row.create_time}}</p>
                            </template>
                        </el-table-column>
                        <!-- <el-table-column
                            align="center"
                            label="提现审核">
                            <template slot-scope="scope">
                                <p>{{scope.row.audit === 0 ? '未审核' : '已审核'}}</p>
                            </template>
                        </el-table-column> -->
                        <el-table-column
                            align="center"
                            label="提现状态">
                            <template slot-scope="scope">
                                <p>{{scope.row.auditing_status === '0' ? '未到账' : scope.row.auditing_status === '1' ? '已到账' : '未到账'}}</p>
                            </template>
                        </el-table-column>
                        <!-- <el-table-column
                            label="操作"
                            align="center">
                            <template slot-scope="scope">
                                <el-button type="text" size="small" @click="look(scope.row.userId)">查看详情</el-button>
                            </template>
                        </el-table-column> -->
                        <!-- <el-table-column
                            label="操作"
                            align="center">
                            <template slot-scope="scope">
                                <el-button type="text" size="small" :disabled="scope.row.auditing_status === '1'" @click="pass(scope.row.id)">通过</el-button>
                                <el-button type="text" size="small" :disabled="scope.row.auditing_status !== '0'" @click="reject(scope.row.id)">驳回</el-button>
                            </template>
                        </el-table-column> -->
                    </el-table>

                    <!-- 导出用表格 不显示 -->
                    <el-table
                        id="table"
                        ref="table"
                        :data="tableData"
                        size="mini"
                        style="width: 100%;min-width: 800px"
                        v-show="false">
                        <!-- <el-table-column
                            type="selection"
                            width="55">
                        </el-table-column> -->
                        <el-table-column
                            align="center"
                            prop="uid"
                            label="用户ID">
                        </el-table-column>
                        <el-table-column
                            align="center"
                            label="用户昵称">
                            <template slot-scope="scope">
                                <p>{{scope.row.user ? scope.row.user.name : '- -'}}</p>
                            </template>
                        </el-table-column>
                        <el-table-column
                            align="center"
                            label="提现金额">
                            <template slot-scope="scope">
                                <p>￥{{scope.row.price}}</p>
                            </template>
                        </el-table-column>
                        <el-table-column
                            align="center"
                            label="提现渠道">
                            <template slot-scope="scope">
                                <p>{{scope.row.bank ?scope.row.bank.bank : '- -'}}</p>
                            </template>
                        </el-table-column>
                        <el-table-column
                            align="center"
                            label="提现账户">
                            <template slot-scope="scope">
                                <p>{{scope.row.user ? scope.row.user.account : '- -'}}</p>
                            </template>
                        </el-table-column>
                        <el-table-column
                            align="center"
                            label="提现时间"
                            min-width="120">
                            <template slot-scope="scope">
                                <p>{{scope.row.create_time}}</p>
                            </template>
                        </el-table-column>
                        <el-table-column
                            align="center"
                            label="提现状态">
                            <template slot-scope="scope">
                                <p>{{scope.row.status === '0' ? '未审核' : scope.row.status === '1' ? '已到账' : '已驳回'}}</p>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>

                <div class="page-box">
                    <Page :total="total" :page-size="pageSize" show-elevator show-total @on-change="pageChange" />
                </div>
            </Content>
            <Footer>
                <p class="copy-right">Copyright © {{host}}, All Rights Reserved.</p>
            </Footer>
        </Layout>
    </div>
    
</template>

<script>
    import * as api from '@/api/api';
    import FileSaver from 'file-saver';
    import XLSX from 'xlsx';
    export default {
        data(){
            return {
                host: window.location.host,
                loading: this.$store.state,
                way: 1,//提现方式 1支付宝  2微信
                userId: '',//用户账号/ID
                userName: '',//用户名称
                money: '',//金额
                isOpen: false,//审核开关
                openChangeLoading: false,//开关请求状态
                userName: '',
                moneyList: [
                    {
                        label: '100元以下',
                        value: '0&100,between'
                    },
                    {
                        label: '100-200元',
                        value: '101&200,between'
                    },
                    {
                        label: '200-500元',
                        value: '201&500,between'
                    },
                    {
                        label: '500-1000元',
                        value: '501&1000,between'
                    },
                    {
                        label: '1000元以上',
                        value: '1001&100000000,between'
                    },
                    {
                        label: '全部',
                        value: '5'
                    }
                ],
                state: '',
                stateList: [
                    {
                        label: '未审核',
                        value: '0'
                    },
                    {
                        label: '已到账',
                        value: '1'
                    },
                    {
                        label: '已驳回',
                        value: '2'
                    },
                    {
                        label: '全部',
                        value: '4'
                    }
                ],
                tableData: [],//表格数据
                total: 6,//数据总条数
                pageSize: 10,//每页显示条数
                current_page: 1,//当前页
            }
        },
        mounted(){
            this.getList();
            // this.getAuditSwitchState();
        },

        computed: {
            
        },

        watch: {
            
        },
        
        methods: {
            pageChange(current_page) {
                this.current_page = current_page;
                this.getList();
            },
            current(i) {
                this.way = i;
                this.current_page = 1;
                this.getList()
            },
            getList() {
                this.loading.isLoading = true;
                if(this.way === 1) {
                    let body = {
                        offset: this.current_page - 1,
                        limit: this.pageSize
                    }
                    if(this.userName) {
                        body.search_name = this.userName;
                    }
                    api.getAliCash(body).then((res) => {
                        if(res.data.code === 200) {
                            this.loading.isLoading = false;
                            this.tableData = res.data.data.data;
                            this.total = res.data.data.count * 1
                        }else {
                            this.$Notice.error({
                                title: '错误',
                                desc: res.data.msg
                            });
                        }
                    }).catch(err => {
                        this.$Notice.error({
                            title: '错误',
                            desc: '服务器出错了'
                        });
                    })
                }else if(this.way === 2) {
                    let body = {
                        offset: this.current_page - 1,
                        limit: this.pageSize
                    };
                    if(this.userName) {
                        body.search_name = this.userName;
                    }
                    api.getWechatCash(body).then(res => {
                        if(res.data.code === 200) {
                            this.loading.isLoading = false;
                            this.tableData = res.data.data.data;
                            this.total = res.data.data.count * 1
                        }else {
                            this.$Notice.error({
                                title: '错误',
                                desc: res.data.msg
                            });
                        }
                    }).catch(err => {
                        this.$Notice.error({
                            title: '错误',
                            desc: '服务器出错了'
                        });
                    })
                }
            },
            exportData() {
                /* generate workbook object from table */
                var wb = XLSX.utils.table_to_book(document.querySelector('#table'))
                /* get binary string as output */
                var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
                try {
                    FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '提现列表.xlsx')
                } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
                return wbout
            },

            //获取审核开关状态
            getAuditSwitchState() {
                // this.loading.isLoading = true;
                api.getAuditSwitch().then((res) => {
                    if(res.data.code === 200) {
                        if(res.data.data.cash_open === 1) {
                            this.isOpen = false;
                        }else {
                            this.isOpen = true;
                        }
                    }
                })
            },

            //审核设置
            openChange() {
                this.openChangeLoading = true;
                let body = {
                    cash_open: this.isOpen ? 0 : 1
                };
                api.auditSwitch(body).then((res) => {
                    if(res.data.code === 200) {
                        this.openChangeLoading = false;
                        this.$Notice.success({
                            title: '提示',
                            desc: '恭喜你，操作成功！'
                        });
                        this.getVideoList();
                    }else {
                        this.$Notice.error({
							title: '错误',
							desc: res.data.msg
                        });
                        this.isOpen = !this.isOpen;
                        this.openChangeLoading = false;
                    }
                }).catch((err) => {
                    this.loading.isLoading = false;
					this.$Notice.error({
						title: '错误',
						desc: '服务器错误,请联系管理员（' + err + '）',
					});
                })
            },

            //通过
            pass(id) {
                this.$confirm('确定要通过该提现申请吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
                    let body = {
                        id,
                        auditing_status: '1'
                    };
                    this.loading.isLoading = true;
                    api.cashAudit(body).then(res => {
                        if(res.data.code === 200) {
                            this.$Notice.success({
                                title: '提示',
                                desc: res.data.msg
                            });
                            this.getList();
                        }else {
                            this.loading.isLoading = false;
                            this.$Notice.error({
                                title: '错误',
                                desc: res.data.msg
                            });
                        }
                    })
                })
            },
            //驳回
            reject(id) {
                let body = {
                    id,
                    auditing_status: '2'
                };
                this.loading.isLoading = true;
                api.cashAudit(body).then(res => {
                    if(res.data.code === 200) {
                        this.$Notice.success({
                            title: '提示',
                            desc: res.data.msg
                        });
                        this.getList();
                    }else {
                        this.$Notice.error({
                            title: '错误',
                            desc: res.data.msg
                        });
                    }
                })
            }
        }  
    }
</script>


<style lang="less">
    @import '../../assets/styles/constant.less';
    .withdrawal{
        width: 100%;
        height: 100%;
        overflow-x: hidden;
        overflow-y: auto;
        background-color: #f5f7f9;
        border-bottom: 1px solid #ddd;
        .ivu-layout{
            min-height: 100%;
        }
        .header{
            font-size: 14px;
            line-height: 40px;
            height: 40px !important;
            color: #434343;
            padding: 0 50px;
            background-color: @background;
            border-bottom: 1px solid #ddd;
            .border-left {
                border-left: 5px solid @baseColor;
                margin-right: 4px;
            }
        }
        .content{
            width: 100%;
            min-height: 76%;
            padding: 20px 60px;
            .selectWay{
                .current{
                    background-color: @greenColor !important;
                    border: 1px solid @greenColor;
                    color: #fff !important;
                }
            }
            .search-box{
                margin-top: 10px;
                .search-btn{
                    width: 100px;
                    height: 32px;
                    line-height: 32px;
                    text-align: center;
                    background-color: @greenColor;
                    border-radius: 4px;
                    color: #fff;
                    cursor: pointer;
                }
            }
            .btn-box{
                margin-top: 10px;
                display: flex;
                justify-content: space-between;
                line-height: 30px;
                .btn{
                    background-color: #fff;
                    border: 1px solid #ddd;
                    color: #333;
                }
            }
            .table-box{
                // margin-top: 10px;
                margin-top: 20px;
                width: 100%;
                .el-table th{
                    background-color: #eee;
                    color: #333;
                    font-size: 14px;
                }
                .el-button--text{
                    color: @greenColor;
                }
            }
            .page-box{
                height: 50px;
                line-height: 50px;
                display: flex;
                justify-content: flex-end;
                background-color: #fff;
                padding: 0 20px;
                
            }
        }
        .copy-right{
            text-align: center;
            color: #aaa;
            font-size: 12px;
        }
    }
</style>